home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / rbbs_pc / 17basm.zip / XMODEM.ASM < prev    next >
Assembly Source File  |  1988-02-03  |  4KB  |  107 lines

  1. ; Modified 8/24/85 for use with QuickBasic Compiler
  2.  
  3. ; Heavy modifications 8/31/86 by Jim King
  4. ; Changed CRC_CALC from the awfulness it was to an algorithm suggested
  5. ; by Philip Burns.  In a test program, this algorithm is over 3 times as
  6. ; fast as the one previously used by RBBS-PC.
  7. ; Changed the loop that calculates checksum and calls the CRC to be more
  8. ; efficient (just about halved the number of instructions).
  9. ; Note that RBBS-PC.BAS was also modified so that it no longer tacks on
  10. ; two null bytes to the input string (they were necessary for the old CRC
  11. ; routine to work correctly).
  12. ; Once again, thanks to Philip Burns for suggesting the CRC algorithm.
  13. ; Many thanks also to John Souvestre, who helped me tweak the assembly
  14. ; routine to run even faster.
  15.  
  16. XM_CALC   SEGMENT PUBLIC 'CODE'
  17.           ASSUME CS:XM_CALC
  18.           PUBLIC XMODEM
  19. ;
  20. CHK_SUM           DB 0
  21. STRG_LEN          DW 0                  ;CHANGED TO LENGTH OF STRING PASSED
  22. STRG_LOC          DW 0
  23. STRG_MSG          DB 1026 DUP (' ')     ;COMMAND CHARS (+CR) GO INTO HERE
  24. ;
  25. ;
  26. ;
  27. XMODEM    PROC    FAR
  28.           PUSH    BP
  29.           MOV     BP,SP
  30.           MOV     CHK_SUM,0         ;INITIALIZE
  31. ;
  32.           MOV     SI,[BP+14]        ;GET STRING DESCRIPTOR
  33.           MOV     BL,[SI+ 2]        ;REARRANGE LOW/HIGH BYTES
  34.           MOV     BH,[SI+ 3]        ;NOW BX HOLDS THE ADDRESS OF THE STRING
  35.           MOV     STRG_LOC,BX       ;STORE IT
  36.           MOV     AX,[SI]           ;GET STRING LENGTH
  37.           MOV     STRG_LEN,AX       ;STORE IT
  38. ;
  39.           MOV     CX,STRG_LEN           ;STORE LENGTH IN CX
  40.           MOV     SI,STRG_LOC           ;STORE OFFSET TO STRING IN SI
  41.           PUSH    CS
  42.           POP     ES
  43.           MOV     DI,OFFSET STRG_MSG    ;ES:DI = LOCATION OF VARIABLE
  44.           REP     MOVSB                 ;FILL STRG_MSG WITH STRING
  45. ;
  46.           PUSH    DS                    ;SAVE DS
  47.           PUSH    CS
  48.           POP     DS
  49.  
  50.           MOV     CX,STRG_LEN           ;INITIALIZE COUNTER
  51.       MOV      SI,OFFSET STRG_MSG    ;get address of input string
  52.           XOR     DX,DX            ;initialize CRC value to 0
  53. LOOP1:
  54.       LODSB                ;get character into AL
  55.           MOV     DI,CX                 ;SAVE CX
  56.           ADD     CHK_SUM,AL            ;ADD AL TO CHK_SUM
  57.  
  58. ; this used to be:
  59. ;CRC_CALC   PROC NEAR
  60. ; this is the CRC calculation routine.  It's placed here instead of in
  61. ; a separate procedure for additional speed.
  62. ; DX contains the CRC value, AL has the new character.  Other registers
  63. ; are used for temporary storage and scratch work.
  64.     XCHG    DH,DL            ; CRC := Swap(CRC) XOR Ord(Ch);
  65.     XOR    DL,AL
  66.  
  67.     MOV    AL,DL            ; CRC := CRC XOR ( Lo(CRC) SHR 4 );
  68.     MOV    CL,4
  69.     SHR    AL,CL
  70.     XOR    DL,AL
  71.  
  72.                     ; CRC := CRC XOR ( Swap(Lo(CRC)) SHL 4 )
  73.                     ;        XOR ( Lo(CRC) SHL 5 );
  74.     MOV    BL,DL
  75.     MOV    AH,DL
  76.     SHL    AH,CL
  77.     XOR    DH,AH
  78.     XOR    BH,BH
  79.     INC    CL
  80.     SHL    BX,CL
  81.     XOR    DX,BX
  82. ; end of the CRC calculation routine
  83.     
  84.           MOV     CX,DI                 ;RESTORE CX
  85.       LOOP      LOOP1            ;do it again
  86.  
  87.  
  88.           POP     DS                   ;RESTORE DS
  89.           MOV     BX,DX                ;PASS BACK THE CRC VALUE
  90.           MOV     SI,[BP+ 6]           ;AND CRC HIGH AND LOW BYTES
  91.           MOV     [SI],BL
  92.           MOV     SI,[BP+ 8]
  93.           MOV     [SI],BH
  94.           MOV     SI,[BP+10]
  95.           MOV     [SI],BX
  96.           MOV     BL,CS:CHK_SUM        ;PASS BACK THE CHECK SUM
  97.           MOV     SI,[BP+12]
  98.           MOV     [SI],BL
  99. ;
  100.           PUSH    CS                ;CLEAN UP WORK TO RETURN TO BASIC
  101.           POP     ES
  102.           POP     BP
  103.           RET     10
  104. XMODEM    ENDP
  105. XM_CALC   ENDS
  106.           END
  107.